﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;
using System.Text.RegularExpressions;
using Entity;
using BAL;
using Common;
namespace Web
{
    public partial class ExamPaerDisp : System.Web.UI.Page
    {
        protected static int ExamPaperId = 0;
        protected static ExamPaperInfo ExamPaper_Info = new ExamPaperInfo();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!UserValidate.CheckLogin(this.Request, "MasterTestPaper"))
            {
                Response.Write("<script type=\"text/javascript\">top.location.href='login.aspx';</script>");
                Response.Redirect("login.aspx");
                Response.End();
            }
            if (Request.QueryString["ExamPaperId"] != null && Request.QueryString["ExamPaperId"].Trim().Length > 0)
            {
                ExamPaperId = int.Parse(Request.QueryString["ExamPaperId"].Trim());
            }
            else
            {
                Response.Redirect("default.aspx");
            }
            if (!IsPostBack)
            {
                ExamPaper_Info = BAL.ExamPaper.GetT1(ExamPaperId);
                LoadQuestion();
            }
        }

        private void LoadQuestion()
        {
            string[] hanzi=new string[]{"一","二","三","四","五","六","七","八","九","十","十一","十二","十三","十四","十五","十六","十七","十八","十九","二十","二十一","二十二","二十三","二十四","二十五","二十六","二十七","二十八","二十九","三十"};
            IList<PaperQuestionInfo> infos = new List<PaperQuestionInfo>();
            infos = Question.GetList(ExamPaperId);
            if (ExamPaper_Info.TestPaperPolicyMainId == null)
            {
                ExamPaper_Info.TestPaperPolicyMainId = 0;
            }
            IList<TestPaperPolicySubInfo> tpinfo = TestPaperPolicySub.GetList((int)ExamPaper_Info.TestPaperPolicyMainId);
            StringBuilder sb = new StringBuilder();
            int i = 0;
            int a = 1;
            foreach (TestPaperPolicySubInfo info in tpinfo)
            {
                XmlQuestion xq = XmlQuestionHelper.GetXmlQuestionInfo(info.Content);
                if (i == 0)
                {
                    sb.Append("<h3 class='question-t first block'>");
                }
                else
                {
                    sb.Append("<h3 class='question-t block'>");
                }
                sb.Append(hanzi[i]);
                sb.Append("、");
                sb.Append(QuesType.GetQuesTypeInfoById(int.Parse(xq.Type)).Name);
                sb.Append("共 ");
                sb.Append(xq.AllCount.ToString());
                sb.Append(" 小题 ");
                if (xq.SingleScore!=null && xq.SingleScore.Count > 0)
                {
                    sb.Append("每小题 ");
                    sb.Append(string.Join(",", ArrayHelper.ConvertToStringArray(GetArray<decimal>.Get(xq.SingleScore))));
                    sb.Append(" 分");
                }
                sb.Append("总分为：");
                sb.Append(xq.AllScore.ToString());
                sb.Append("分");
                sb.Append("</h3>");
                IList<PaperQuestionInfo> tInfos = infos.Where(t => t.SubPolicyId == info.Id).ToList();
                foreach (PaperQuestionInfo qinfo in tInfos)
                {
                    sb.Append("<div class='block' id='block");
                    sb.Append(a.ToString());
                    sb.Append("'><div class='blockleft'>");
                    sb.Append(a.ToString());
                    sb.Append(".</div><div class='blockright'>");
                    qinfo.Title = ClearHtml(qinfo.Title);
                    if (xq.SingleScore.Count > 1)
                    {
                        qinfo.Title = WiterScore(qinfo.Title, qinfo.Score);
                    }
                    sb.Append(qinfo.Title);
                    sb.Append("<div class='Answer'>");
                    sb.Append(qinfo.Answer);
                    sb.Append("</div></div><div class='isclear'></div></div>");
                    a++;
                }
                i++;
            }
            TempList.Text = sb.ToString();
        }

        private string WiterScore(string InputStr, decimal Score)
        {
            string t = InputStr;
            string[] strArray = new Regex("<.[^>]*?>(.*?)</.*?>", RegexOptions.IgnoreCase).Split(t);
            foreach (string item in strArray)
            {
                string temp = CodingHtml(item);
                temp = Regex.Replace(temp, "{{.*?}}", "");
                if (temp.Trim().Length > 0)
                {
                    if (!temp.Trim().StartsWith("{{"))
                    {
                        if (t.IndexOf(temp.Trim()) > -1)
                        {
                            t = t.Replace(temp, "(" + Score.ToString() + ")" + temp);
                            return t;
                        }
                    }
                }
            }
            return t;
        }

        /// <summary>
        /// 编码Html
        /// </summary>
        /// <param name="InputStr">输入字符串</param>
        /// <returns></returns>
        private string CodingHtml(string InputStr)
        {
            if (InputStr.Length > 0)
            {
                string t = InputStr;
                t = t.Replace("&nbsp;", "{{空格}}");
                t = Regex.Replace(t, "<(.*?)>", "{{($1)}}");
                return t;
            }
            else
            {
                return InputStr;
            }
        }

        /// <summary>
        /// 解码Html
        /// </summary>
        /// <param name="InputStr"></param>
        /// <returns></returns>
        private string DecodingHtml(string InputStr)
        {
            if (InputStr.Length > 0)
            {
                string t = InputStr;
                t = t.Replace("{{空格}}", "&nbsp;");
                t = Regex.Replace(t, "{{(.*?)}}", "<$1>");
                return t;
            }
            else
            {
                return InputStr;
            }
        }

        private string ClearHtml(string InputStr)
        {
            string Temp = Module_String.clearhtml(InputStr, "span");
            Temp = Temp.Replace("<p>&nbsp;</p>", "");
            Temp = Module_String.DelStr("style=\"", "\"", Temp);
            Temp = Module_String.DelStr("class=\"", "\"", Temp);
            return Temp;
        }

        /// <summary>
        /// 将Web控件导出
        /// </summary>
        /// <param name="source">控件实例</param>
        public void ExpertControl(System.Web.UI.Control source)
        {
            Response.AppendHeader("Content-Disposition", "attachment;filename=result.doc");
            Response.ContentType = "application/ms-word";
            Response.Charset = "UTF-8";
            Response.ContentEncoding = System.Text.Encoding.UTF8;
            source.Page.EnableViewState = false;
            System.IO.StringWriter writer = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter htmlWriter = new System.Web.UI.HtmlTextWriter(writer);
            source.RenderControl(htmlWriter);
            Response.Write(writer.ToString());
            Response.End();
        }
    }
}
